﻿#include "SystemInfoController.h"

SystemInfoController::SystemInfoController(){}

SystemInfoController::~SystemInfoController(){}

void SystemInfoController::viewDidLoad(){
	CAView* view=this->getView();
	this->setNavigationBarItem(CANavigationBarItem::create("系统通知"));

	list=CACollectionView::createWithLayout(DLayoutFill);
	list->setHoriInterval(0);
	list->setVertInterval(20);
	list->setAllowsSelection(false);
	list->setHeaderRefreshView(CAPullToRefreshView::create(CAPullToRefreshView::Type::Header));
	list->setFooterRefreshView(CAPullToRefreshView::create(CAPullToRefreshView::Type::Footer));
	list->onCellAtIndexPath(CALLBACK_BIND_4(SystemInfoController::collectionCellAtIndex, this));
	list->onCellHeightAtIndexPath([](unsigned int section, unsigned int row){
		return 140;
	});
	list->onNumberOfRowsAtIndexPath([&](unsigned int section){
		return datas.size();
	});
	list->onNumberOfItemsAtIndexPath([](unsigned int section, unsigned int row){
		return 1;
	});
	list->onHeaderBeginRefreshing(CALLBACK_BIND_0(SystemInfoController::scrollViewHeaderBeginRefreshing, this));
	list->onFooterBeginRefreshing(CALLBACK_BIND_0(SystemInfoController::scrollViewFooterBeginRefreshing, this));
	view->addSubview(list);
	sendHttp(view, url, obj2str("page=0&size=10&userId=%u", userId), CALLBACK_BIND_1(SystemInfoController::fillData, this));
}

void SystemInfoController::scrollViewHeaderBeginRefreshing(){
	page=0;
	sendHttp(this->getView(), url, obj2str("page=0&size=10&userId=%u", userId), CALLBACK_BIND_1(SystemInfoController::fillData, this));
}

void SystemInfoController::scrollViewFooterBeginRefreshing(){
	sendHttp(this->getView(), url, obj2str("page=%d&size=10&userId=%u", ++page, userId), CALLBACK_BIND_1(SystemInfoController::fillData, this));
}

CACollectionViewCell* SystemInfoController::collectionCellAtIndex(const DSize& cellSize, unsigned int section, unsigned int row, unsigned int item){
	CACollectionViewCell* cell=list->dequeueReusableCellWithIdentifier("system_cell");
	if(cell==nullptr){
		cell=CACollectionViewCell::create("system_cell");
		CALabel* time=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(500, 0.5), DVerticalLayout_H_C(20, 0.1)));
		time->setTag(99);
		time->setFontSize(font.fontSize);
		time->setTextAlignment(CATextAlignment::Center);
		time->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
		cell->getContentView()->addSubview(time);
		CAView* msg=CAView::createWithLayout(DLayout(DHorizontalLayout_L_R(20, 20), DVerticalLayout_H_C(100, 0.6)));
		msg->setTag(100);
		CALabel* content=CALabel::createWithLayout(DLayoutFill);
		content->setTag(101);
		content->setFontSize(font.fontSize);
		content->setTextAlignment(CATextAlignment::Left);
		content->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
		msg->addSubview(content);
		cell->getContentView()->addSubview(msg);
	}
	CSJson::Value val=datas[row];
	CALabel* time=(CALabel*)cell->getContentView()->getSubviewByTag(99);
	time->setText(val["createTime"].asCString());
	CAView* msg=cell->getContentView()->getSubviewByTag(100);
	CALabel* content=(CALabel*)msg->getSubviewByTag(101);
	content->setText(val["content"].asCString());

	return cell;
}

void SystemInfoController::fillData(const string& data){
	CSJson::Reader reader;
	CSJson::Value value;
	bool flag=reader.parse(data, value);
	if(!flag){
		getAlert(this->getView(), "数据解析异常, 请稍后重试");

		return;
	}
	int size=value.size();
	if(size>0){
		if(page==0){
			datas=value;
		}else{
			for(int i=0; i<size; i++){
				datas.append(value[i]);
			}
		}
	}
	list->reloadData();
}